﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <QCheckBox>
#include <stdio.h>
#include <iostream>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::SlotGetOneFrame(QImage image)
{
    m_image = image;
    this->update();
}
void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    if (m_image.size().width() <= 0)
        return;
    QImage img = m_image.scaled(ui->labelVideo->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    int x = ui->labelVideo->geometry().x();
    int y = ui->labelVideo->geometry().y();
    painter.drawImage(QPoint(x, y), img);
}
void MainWindow::on_pushButton_clicked()
{
    ui->pushButton->setEnabled(false);
    QTimer *pTimer = new QTimer(this);
    pTimer->setInterval(100);
    std::cout<<"MainWindow thread id = "<<QThread::currentThreadId()<<"\n";
    QString strRtspUrl = ui->lineEdit_url->text();
    if(strRtspUrl == "")
    {
        return;
    }
    QString strOut = ui->lineEdit_addr->text();
    QThread *pThread = new QThread(this);
    m_pPlayVideo = new playVideo(strRtspUrl,strOut);
    m_pPlayVideo->moveToThread(pThread);
    connect(this,&MainWindow::activateRec, m_pPlayVideo,&playVideo::setRecActive);
    connect(this,&MainWindow::deActivateRec, m_pPlayVideo,&playVideo::setRecDeActive);
    connect(this, &MainWindow::rtspUrlReady, m_pPlayVideo,&playVideo::playstart);
    connect(m_pPlayVideo, &playVideo::Sig_GetFrames, this, &MainWindow::SlotGetOneFrame);
    emit rtspUrlReady();
    pThread->start();
}
